<?php

namespace app\admin\model\cms;
use app\common\model\User as UserModel;
use app\admin\model\cms\Channels;
use think\Model;


class News extends Model
{
    // 表名
    protected $name = 'news';
    
  // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    protected $publishedTime = 'published_time';
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'published_time_text',
        'delete_time_text',
        'channel_text',
        'status_text',
        'user_text'
    ];
    /**
     * 关联 user表
     * @return \think\model\relation\BelongsTo
     */
//    public function user()
//    {
//        return $this->belongsTo('UserModel', 'user_id');
//    }
    public function user(){
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->field('id,username,nickname,avatar,bio,email')->setEagerlyType(1);
    }

    /**
     * 关联分类表
     * @return \think\model\relation\BelongsToMany
     */

    public function channel()
    {

        return $this->belongsTo('NewsCategoryModel', 'parent_id', '', [], 'LEFT')->setEagerlyType(0);
    }
    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPublishedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['published_time']) ? $data['published_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPublishedTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function getPostContentAttr($value)
    {
        if (empty($value)) {
            return '';
        }
//        return cmf_replace_content_file_url(htmlspecialchars_decode($value));
        return htmlspecialchars_decode($value);
    }
     /**
     * post_content 自动转化
     * @param $value
     * @return string
     */
    public function setPostContentAttr($value)
    {
        if (empty($value)) {
            return '';
        }
//        return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
        return htmlspecialchars(htmlspecialchars_decode($value));
    }
    public static function getPostStatusList()
    {
        return ['1' => __('Zh'), '0' => __('Yc')];
    }

    public function getChannelTextAttr($value, $data)
    {
        $value = $value ? $value : $data['parent_id'];
        $name  = NewsCategoryModel::where('id', '=', $value)->value('platename');
        return !empty($name) ? $name:'';
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['post_status'];
        $list = $this->getPostStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getUserTextAttr($value, $data)
    {
        $value = $value ? $value : $data['user_id'];
        $username = UserModel::where('id','=',$value)->value('username');
        return !empty($username) ? $username : '';
    }

}
